package view;

import javax.swing.JPanel;

import controller.ACSystem;
import model.Auction;
import model.Bid;
import model.Bidder;
import model.Item;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * 
 * @author Bob Landowski
 * 
 */
@SuppressWarnings("serial")
public class ItemPane extends JPanel {

	private int my_item_number;
	private Auction my_auction;

	private String my_auction_owner;
	private String my_description;

	private Item my_item;

	private JLabel my_auctionName;
	private JLabel my_quantityField;
	private JLabel itemnumberLabel;
	private JLabel currentBidField;
	private String my_bid;
	private Bidder my_bidder;

	private JButton bidButton;

	private JTextArea my_itemDescription;

	private ACSystem my_ACcontroller;

	public ItemPane(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		setLayout(null);
		setPreferredSize(new Dimension(560, 560));

		JLabel auctionCentralItemLabel = new JLabel(
				"Auction Central Item Display");
		auctionCentralItemLabel.setBounds(110, 16, 223, 20);
		add(auctionCentralItemLabel);

		JLabel auctionNameLabel = new JLabel("Auction Name:");
		auctionNameLabel.setBounds(15, 54, 106, 20);
		add(auctionNameLabel);

		my_auctionName = new JLabel(my_auction_owner);
		my_auctionName.setBounds(147, 54, 300, 20);
		add(my_auctionName);

		JLabel itemDescriptionLabel = new JLabel("Description:");
		itemDescriptionLabel.setBounds(15, 174, 106, 20);
		add(itemDescriptionLabel);

		JLabel quantityLabel = new JLabel("Quantity:");
		quantityLabel.setBounds(15, 134, 69, 20);
		add(quantityLabel);

		my_quantityField = new JLabel();
		my_quantityField.setBounds(147, 134, 176, 20);
		add(my_quantityField);

		my_itemDescription = new JTextArea();
		my_itemDescription.setLineWrap(true);
		my_itemDescription.setWrapStyleWord(true);
		my_itemDescription.setText(my_description);
		my_itemDescription.setBounds(147, 174, 250, 100);
		add(my_itemDescription);

		JLabel currentBidLabel = new JLabel("Current Bid:");
		currentBidLabel.setBounds(15, 294, 106, 20);
		add(currentBidLabel);

		bidButton = new JButton("Place Bid");
		bidButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				my_bid = JOptionPane.showInputDialog("Enter your bid.");
				if (my_bid != null) {
					my_bidder = (Bidder) my_ACcontroller.getCurrentUser();
					Bid my_current_bid = new Bid(my_item, my_bidder, Double
							.parseDouble(my_bid));
					my_item.addBid(my_current_bid);
					my_bidder.placeBid(my_current_bid);
					currentBidField.setText(Double.toString(my_item
							.highestBid().getBid()));

				} else {
				}

			}
		});
		bidButton.setBounds(255, 294, 115, 29);
		add(bidButton);

		JLabel lblItemNumber = new JLabel("Item Number:");
		lblItemNumber.setBounds(15, 94, 106, 20);
		add(lblItemNumber);

		itemnumberLabel = new JLabel(my_item_number + "");
		itemnumberLabel.setBounds(147, 94, 300, 20);
		add(itemnumberLabel);

		currentBidField = new JLabel();
		currentBidField.setBounds(133, 294, 69, 20);
		add(currentBidField);
		my_ACcontroller = the_ACcontroller;
	}

	public void getObjectData(Object the_object) {
		my_item = (Item) the_object;
		my_auction = my_item.getAuction();


		my_description = my_item.getMyDescription();


		itemnumberLabel.setText(Integer.toString(my_item.getMyItemNumber()));
		currentBidField.setText(Double.toString(my_item.highestBid().getBid()));
		my_quantityField.setText(my_item.getQuantity()+"");
		my_auctionName.setText(my_auction.getOwner().getMy_organization());
		my_itemDescription.setText(my_description);
		if (my_ACcontroller.getCurrentUser().getMy_accessLevel() != 4) {
			bidButton.setEnabled(false);
		}
	}
}
